<?php
/**
 * Created by PhpStorm.
 * User: Shinelon
 * Date: 2021/4/17
 * Time: 17:45
 */

namespace app\admin\controller;


use app\admin\model\GoodsCate;
use app\admin\model\GoodsSpe;
use app\AdminRest;
use think\App;
use app\admin\model\Goods as GoodsModel;
use think\facade\View;

class Goods extends AdminRest
{
    protected $model;

    public function __construct(App $app, GoodsModel $model)
    {
        parent::__construct($app);
        $this->model = $model;
    }

    public function addGoods()
    {
        try {
            validate($this->model->rule, $this->model->message)->check($this->_param);
            $data = $this->_param;
            $data['create_time'] = $data['update_time'] = time();
            $this->model->save($data);
            return $this->success('添加成功');
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function goodsList()
    {
        $needData = $this->_param['needData'] ?? 0;
        switch ($needData) {
            case  1:
                $where = ['deleted' => 0];
                if (isset($this->_param['status']))
                    $where['status'] = $this->_param['status'];
                $keyword = '';
                if (isset($this->_param['keyword']) && !empty($this->_param['keyword']))
                    $keyword = $this->_param['keyword'];
                $goods = $this->model->when(!empty($keyword),
                    function ($q) use ($keyword) {
                        $q->where('name', 'like', "%$keyword%");
                    })->where($where)->order('sort', 'desc')
                    ->paginate($this->_param['size'] ?? 10);
                return $this->success($goods);
                break;
            default:
                return View::fetch('goodsList');
                break;
        }
    }

    public function editGoods()
    {
        $data = $this->_param;
        unset($data['id']);
        if (empty($this->_param['id']) || $this->_param['id'] < 0)
            return $this->error('数据有误');
        try {
            validate($this->model->rule, $this->model->message)->check($data);
            $data['update_time'] = time();
            if (isset($data['deleted']) && $data['deleted'] == 1)
                $data['delete_time'] = $data['update_time'];
            unset($data['create_time']);
            $this->model->where(['id' => $this->_param['id']])->save($data);
            return $this->success('修改成功');
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function specificationsRelationship(GoodsSpe $model)
    {
        if (empty($this->_param['goodsId']))
            return $this->error('数据缺失');
        $data = $model->where(['goods_id' => $this->_param['goodsId']])->select()->toArray();
        return $this->success(array_column($data, 'spe_id'));
    }

    public function editSpecificationsRelationship(GoodsSpe $model)
    {
        if (empty($this->_param['goodsId']))
            return $this->error('数据缺失');
        if (!empty($this->_param['add'])) {
            $insertData = $this->_param['add'];
            $insertData = array_column($insertData, null, 'spe_id');
            $alreadyInsert = $model->where(['goods_id' => $this->_param['goodsId']])->select()->toArray();
            foreach ($alreadyInsert as $value) {
                if (isset($insertData[$value['spe_id']]))
                    unset($insertData[$value['spe_id']]);
            }
            if(!empty($insertData))
                $model->insertAll(array_values($insertData));
        }
        if (!empty($this->_param['del']))
            $model->where(['goods_id' => $this->_param['goodsId']])->whereIn('spe_id', $this->_param['del'])->delete();
        return $this->success('绑定成功');
    }

    public function cateRelationship(GoodsCate $model)
    {
        if (empty($this->_param['cateId']))
            return $this->error('数据缺失');
        $data = $model->where(['cate_id' => $this->_param['cateId']])->select()->toArray();
        return $this->success(array_column($data, 'goods_id'));
    }

    public function editCateRelationship(GoodsCate $model)
    {
        if (empty($this->_param['cateId']))
            return $this->error('数据缺失');
        if (!empty($this->_param['add']))
            $model->insertAll($this->_param['add']);
        if (!empty($this->_param['del']))
            $model->where(['cate_id' => $this->_param['cateId']])->whereIn('goods_id', $this->_param['del'])->delete();
        return $this->success('绑定成功');
    }
}
